package cn.xsly.user.controller;

import cn.xsly.basic.exception.BusinessException;
import cn.xsly.user.dto.EmailDto;
import cn.xsly.user.dto.UserDto;
import cn.xsly.user.service.IUserService;
import cn.xsly.user.domain.User;
import cn.xsly.user.query.UserQuery;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody UserDto dto) {
        try {
            userService.phoneRegister(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return AjaxResult.me().setMessage("注册失败"+e.getMessage());
        } catch (Exception e) {
            return AjaxResult.me().setMessage("系统错误"+e.getMessage());
        }
    }
    @PostMapping("/register/email")
    public AjaxResult emailRegister(@RequestBody EmailDto dto) {
        try {
            userService.emailRegister(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return AjaxResult.me().setMessage("注册失败"+e.getMessage());
        } catch (Exception e) {
            return AjaxResult.me().setMessage("系统错误"+e.getMessage());
        }
    }


    /**
     * 接口：添加或修改
     *
     * @param user 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user) {
        try {
            if (user.getId() != null)
                userService.update(user);
            else
                userService.add(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            userService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Long id) {
        return userService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }
}
